package io.renren.modules.wx.controller;

import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import global.hh.algorithms.lottery.Luck;
import global.hh.algorithms.lottery.util.LuckUtil;
import io.renren.common.utils.R;
import io.renren.modules.wx.dao.LotteryRecordDao;
import io.renren.modules.wx.entity.LotteryRecordEntity;
import io.renren.modules.wx.entity.PrizeEntity;
import io.renren.modules.wx.service.LotteryRecordService;
import io.renren.modules.wx.service.PrizeService;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: lilanfeng
 * @Date: 2019/5/10 15:05
 * @Description:
 */
@RestController
@RequestMapping("wx/luckDraw")
public class LuckDrawController {

  @Autowired
  private PrizeService prizeService;

  @Autowired
  private LotteryRecordService lotteryRecordService;

  @Autowired
  private LotteryRecordDao lotteryRecordDao;




  public R draw(String openid) {

    Map<String,Object> map=new HashMap<>();
    map.put("openid",openid);

    List<LotteryRecordEntity> lotteryRecordEntities = lotteryRecordDao.selectByMap(map);




    List<PrizeEntity> list = prizeService.list();
    List<Prize> prizes = new ArrayList<>();
    for (PrizeEntity entity : list) {
      prizes.add(new Prize(entity.getId(), entity.getName(), String.valueOf(entity.getChance())));
    }

    Prize choose = LuckUtil.choose(prizes);

    return R.ok().put("prize", choose);
  }

  static class Prize implements Luck {

    /**
     *
     */
    private static final long serialVersionUID = 5709773408639033589L;
    private Integer id;
    private String name;
    private String chance;

    public Prize(Integer id, String name, String chance) {
      super();
      this.id = id;
      this.name = name;
      this.chance = chance;
    }

    public Integer getId() {
      return id;
    }

    public Prize setId(Integer id) {
      this.id = id;
      return this;
    }

    public String getName() {
      return name;
    }

    public void setName(String name) {
      this.name = name;
    }

    @Override
    public String getChance() {
      return chance;
    }

    public void setChance(String chance) {
      this.chance = chance;
    }

    @Override
    public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((chance == null) ? 0 : chance.hashCode());
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      return result;
    }

    @Override
    public boolean equals(Object obj) {
      if (this == obj) {
        return true;
      }
      if (obj == null) {
        return false;
      }
      if (getClass() != obj.getClass()) {
        return false;
      }
      Prize other = (Prize) obj;
      if (chance == null) {
        if (other.chance != null) {
          return false;
        }
      } else if (!chance.equals(other.chance)) {
        return false;
      }
      if (name == null) {
        if (other.name != null) {
          return false;
        }
      } else if (!name.equals(other.name)) {
        return false;
      }
      return true;
    }

  }


}
